<template>
  <div class="base-container">
    <t-form ref="form" :data="queryParams" label-width="80" @submit="()=>{this.pagination.current=1;this.getList()}" @reset="()=>{this.pagination.current=1;this.getList()}">
      <t-row style="border-bottom: 1px solid #E5E6EB;margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <t-form-item label="项目名称" name="projectName">
            <t-input
              v-model="queryParams.projectName"
              clearable
              placeholder="请输入项目名称"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="项目编号" name="projectNum">
            <t-input
              v-model="queryParams.projectNum"
              clearable
              placeholder="请输入项目编号"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="承担单位" name="declareDeptName">
            <t-input
              v-model="queryParams.declareDeptName"
              clearable
              placeholder="请输入承担单位"
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item v-if="site_tag === 'jtt'" label="计划类别" name="projectCategory">
            <t-tree-select filterable expand-all clearable v-model="queryParams.projectCategory"  style="width: 240px" :treeProps="{keys: {value: 'id',label:'name'}}"
                           :data="projectCategoryOptions" ></t-tree-select>
          </t-form-item>
          <t-form-item label="学科分类" name="subjectClassification">
            <t-select  style="width: 240px" clearable v-model="queryParams.subjectClassification"
                       :options="dict.type.subject_classification">
            </t-select>
          </t-form-item>
          <t-form-item label="项目类型" name="projectType">
            <t-select  style="width: 240px" clearable v-model="queryParams.projectType"
                       :options="dict.type.project_type">
            </t-select>
          </t-form-item>
          <t-form-item label="项目状态" name="projectStatus">
            <t-select  style="width: 240px" clearable v-model="queryParams.projectStatus"
                       :options="dict.type.project_status">
            </t-select>
          </t-form-item>
          <t-form-item label="验收状态" name="acceptanceStatus">
            <t-select  style="width: 240px" clearable v-model="queryParams.acceptanceStatus"
                       :options="dict.type.acceptance_status">
            </t-select>
          </t-form-item>
          <t-form-item label="建议经费总概算" >
            <t-form-item name="startAdviceFunding">
              <t-input
                v-model="queryParams.startAdviceFunding"
                clearable
                placeholder="大于等于"
                style="width: 120px"
              /> </t-form-item>-<t-form-item name="endAdviceFunding"> <t-input
            v-model="queryParams.endAdviceFunding"
            clearable
            placeholder="小于等于"
            style="width: 120px"
          />
          </t-form-item>
          </t-form-item>
          <t-form-item :label="site_tag==='jtt'?'厅拨经费':'集团补助经费'" >
            <t-form-item name="startSubsidyFunding">
              <t-input
                v-model="queryParams.startSubsidyFunding"
                clearable
                placeholder="大于等于"
                style="width: 120px"
              />
            </t-form-item> - <t-form-item name="endSubsidyFunding">
            <t-input
              v-model="queryParams.endSubsidyFunding"
              clearable
              placeholder="小于等于"
              style="width: 120px"
            />
          </t-form-item>
          </t-form-item>
          <t-form-item label="项目负责人" name="projectLeaderName">
            <t-input
              v-model="queryParams.projectLeaderName"
              clearable
              style="width: 240px"
            />
          </t-form-item>
          <t-form-item label="项目成员" name="projectMemberName">
            <t-input
              v-model="queryParams.projectMemberName"
              clearable
              style="width: 240px"
            />
          </t-form-item>

          <t-form-item label="任务书时间" name="contractDateRange">
            <t-date-range-picker style="width: 280px" :placeholder="['开始时间','结束时间']" v-model="queryParams.contractDateRange"  allow-input clearable/>
          </t-form-item>
          <t-form-item label="申请验收日期" name="projectDateRange">
            <t-date-range-picker style="width: 280px" v-model="queryParams.projectDateRange" :placeholder="['开始时间','结束时间']" allow-input clearable/>
          </t-form-item>
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit"> 查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <t-table
      rowKey="id"
      size="small"
      :data="data"
      :columns="site_tag === 'jtt'?columns:columns.filter(val=>{ return  val.title!=='计划类别'})"
      hover
      :pagination="pagination"
      :loading="dataLoading"
      :selected-row-keys="selectedRowKeys"
    >
      <template #ysReviewStatus="{row}">
        {{ row.ysReviewStatus | filterByDict(dict.type.kjxm_ys_review_status) }}
      </template>
      <template #adviceFunding="{row}">
        {{row.adviceFunding}}万元
      </template>
      <template #subsidyFunding="{row}">
        {{row.subsidyFunding}}万元
      </template>
      <template #projectName="{ row }">
        <t-link v-if="row.version === '2024'" @click="dialog.row=row;dialog.objId=row.id;dialog.header=row.projectName;dialog.open_item_detail=true" hover="color" theme="primary">{{row.projectName}}</t-link>
        <t-link v-else @click="dialog.row=row;dialog.objId=row.id;dialog.header=row.projectName;dialog.open_project_detail=true" hover="color" theme="primary">{{row.projectName}}</t-link>
      </template>
      <template #subjectClassification="{ row }">
        {{ row.subjectClassification | filterByDict(dict.type.subject_classification) }}
      </template>
      <template #projectType="{ row }">
        {{ row.projectType | filterByDict(dict.type.project_type) }}
      </template>
      <template #op="{ row }">
<!--        <t-button v-if="['1','4',null].includes(row.acceptanceStatus)"  @click="apply(row)" size="small" variant="text" class="t-button-link">-->
<!--          <t-icon name="edit-1" slot="icon"/>-->
<!--          验收申请-->
<!--        </t-button>-->
        <t-button @click="dialog.row=row;dialog.objId=row.id;dialog.header=row.projectName;dialog.open=true" size="small" variant="text" class="t-button-link">
          <t-icon name="info-circle" slot="icon"/>
          验收详情
        </t-button>
      </template>
      <template #projectStatus="{ row }">
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true" class="cursor-pointer"  :theme="handleTheme(row.projectStatus, dict.type.project_status)" variant="light">
          {{ row.projectStatus | filterByDict(dict.type.project_status) }}
        </t-tag>
      </template>
      <template #acceptanceStatus="{ row }">
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true" class="cursor-pointer" v-if="row.acceptanceStatus" :theme="handleTheme(row.acceptanceStatus,dict.type.acceptance_status)" variant="light">
          {{ row.acceptanceStatus | filterByDict(dict.type.acceptance_status) }}
        </t-tag>
        <t-tag @click="dialog.header=row.projectName;dialog.objId=row.id;dialog.open_approve_list=true" class="cursor-pointer" v-else theme="warning" variant="light">未提交</t-tag>
      </template>
    </t-table>
    <project-detail-dialog tabDefaultValue="5" :row="dialog.row" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_project_detail" v-if="dialog.open_project_detail"></project-detail-dialog>
    <item-detail-dialog tabDefaultValue="6" :row="dialog.row" :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_item_detail" v-if="dialog.open_item_detail"></item-detail-dialog>
    <acceptance-detail-dialog :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open" v-if="dialog.open"></acceptance-detail-dialog>
    <approve-list :header="dialog.header" :objId="dialog.objId" :visible.sync="dialog.open_approve_list" v-if="dialog.open_approve_list"></approve-list>
  </div>
</template>

<script>
import ApproveList from "../approve/components/approve-list.vue";
import acceptanceDetailDialog from "./components/acceptance-detail-dialog.vue";
import ProjectDetailDialog from "../pm/components/project-detail-dialog.vue";
import ItemDetailDialog from "../pm/components/item-detail-dialog.vue";

export default {
  name: "index",
  dicts:["project_type", "subject_classification", "sys_user_sex", "prof_tit", "degree", "duty", "platform_type", "member_type", "project_status", "acceptance_status", "kjxm_ys_review_status"],
  components:{
    ApproveList,
    acceptanceDetailDialog,
    ProjectDetailDialog,
    ItemDetailDialog
  },
  data(){
    return {
      site_tag: import.meta.env.VITE_SITE_TAG,
      projectCategoryOptions: [],
      queryParams:{
        contractDateRange: [],
        projectDateRange: [],
        declareDateRange: [],
        declareDate: '',
        ysStartDate: '',
        ysEndDate: '',
        rwsStartDate: '',
        rwsEndDate: '',
        startDeclareDate: '',
        endDeclareDate: '',
        startAdviceFunding: null,
        endAdviceFunding: null,
        startSubsidyFunding: '',
        endSubsidyFunding: '',
        projectLeaderName: '',
        projectMemberName: '',
        multilevelDeptId: null,
        pageNum: 1,
        pageSize: 10,
        projectCategory: null,
        projectName: '',
        projectType: '',
        projectNum: '',
        projectStatus: '',
        acceptanceStatus: '',
        subjectClassification: '',
        dateRange:[],
        sortGroup: '',
        deptName: '',
        declareDeptName: ''
      },
      selectedRowKeys:[],
      selectedRowObj:{},
      data:[],
      columns:[
        {
          colKey: "projectNum",
          title: "项目编号",
          align: "center",
          width: 120,
          sorter: true,
          // ellipsis: true,
        },
        {
          colKey: "projectName",
          title: "项目名称",
          align: "center",
          width: 220
          // ellipsis: true,

        },
        {
          colKey: "ysReviewStatus",
          title: "验收结论",
          align: "center",
        },
        {
          colKey: "acceptanceDate",
          title: "申请验收日期",
          align: "center",
          // ellipsis: true,
          width: 140,
          sorter: true,
        },
        {
          colKey: "adviceFunding",
          title: "建议经费总概算",
          align: "center",
          width: 160,
          sorter: true,
        },
        {
          colKey: "subsidyFunding",
          title: import.meta.env.VITE_SITE_TAG==='jtt'?"厅拨经费":`集团补助经费`,
          align: "center",
          sorter: true,
          width: 140,
        },
        {
          colKey: "projectLeaderName",
          title: "项目负责人",
          align: "center",
          width: 100
        },
        {
          colKey: "declareDeptName",
          title: "承担单位",
          align: "center",
          width: 180,
          sorter: true
        },
        {
          colKey: "projectCategoryName",
          title: "计划类别",
          align: "center",
          width: 120
        },
        {
          colKey: "subjectClassification",
          title: "学科分类",
          align: "center",
          width: 100
        },
        {
          colKey: "projectType",
          title: "项目类型",
          align: "center",
          width: 120
        },
        {
          colKey: "contractStartDate",
          title: "任务书开始时间",
          align: "center",
          sorter: true,
          width: 140,
        },
        {
          colKey: "contractCompleteDate",
          title: "任务书结束时间",
          align: "center",
          sorter: true,
          width: 140,
        },
        // {
        //   colKey: "attributeDeptName",
        //   title: "归口管理单位",
        //   align: "center"
        // },
        {
          colKey: "projectStatus",
          title: "项目状态",
          align: "center",
          width:220,
          fixed: 'right',
        },
        {
          colKey: "acceptanceStatus",
          title: "验收状态",
          align: "center",
          width: 120,
          fixed: 'right',
        },
        {
          colKey: "op",
          title: "操作",
          align: "center",
          width: 180,
          fixed: 'right',
        },
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination,pageInfo)
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList()
        },
      },
      dialog: {
        contractId: 0,
        open_add_edit: false,
        header: "",
        projectId:undefined,
        row: {},
        detailcode:undefined,
        open_approve_list: false,
        open_project_detail: false,
        open: false,
        open_item_detail: false,
      },
    }
  },
  created() {
    this.getList()
    // this.getDeclareTree();
  },
  methods: {
    apply(row){
      if(row.acceptanceId){
        sessionStorage.removeItem(`${row.id}-acceptance-activeForm`);
        sessionStorage.setItem(`${row.id}-acceptanceId`, row.acceptanceId);
        sessionStorage.setItem(`${row.id}-acceptance-acceptanceId`, row.acceptanceId);
        this.$router.push(`/kjxm/other/acceptance/acceptance-apply/${row.id}`);
      }else {
        this.$router.push(`/kjxm/other/acceptance/acceptance-apply/${row.id}`);
      }
    },
    getList(){
      this.dataLoading = true;
      [this.queryParams.ysStartDate,this.queryParams.ysEndDate] = this.queryParams.projectDateRange;
      [this.queryParams.rwsStartDate,this.queryParams.rwsEndDate] = this.queryParams.contractDateRange;
      this.$api.kjxm.acceptance.myAcceptedList(this.queryParams).then(response=>{
        this.pagination.total = response.total;
        this.data = response.rows;
        this.dataLoading = false;
      }).catch((e) => {
        this.dataLoading = false;
        this.$message.error(e.toString());
      });
    },
    getDeclareTree() {
      this.$api.kjxm.project.planCategoryList().then(response => {
        this.projectCategoryOptions = this.handleTree(response.data, "id");
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
  },
}
</script>

<style scoped>

</style>
